/*
 * HublistWriter.java
 *
 * Created on 12 November 2006, 11:34
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.hublist.db;


import hubsniffer.db.DataBaseManager;
import hubsniffer.db.util.Util;
import libjdc.hublist.HubEntry;
import hubsniffer.hublist.Hublist;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * @author Daniel Botelho
 */
public class HublistWriter {
    private DataBaseManager db; 
    
    /**
     * Cria um HublistWriter com uma BD passada como argumento
     * @param db 
     */
    public HublistWriter(DataBaseManager db) {
        this.db = db;
    }
    
    //private String lastMessage;
    private void insertHubEntry(int hublist_id, HubEntry hub) throws UnsupportedEncodingException, SQLException{
        
        String name = Util.escapeString(hub.getName())+" ";
        String address = Util.escapeString(hub.getAddress().toLowerCase());
        String description = Util.escapeString(hub.getDescription())+" ";
        int users = hub.getUsers();
        String country = Util.escapeString(hub.getCountry());
        String shared = Util.escapeString(hub.getShared());
        String status = Util.escapeString(hub.getStatus());
        String min_share = Util.escapeString(hub.getMin_share());
        String min_slots = Util.escapeString(hub.getMin_slots());
        String max_users = Util.escapeString(hub.getMax_users());
        String max_hubs = Util.escapeString(hub.getMax_hubs());
        String reliability = Util.escapeString(hub.getReliability());
        String rating = Util.escapeString(hub.getRating());
        String port = Util.escapeString(hub.getPort());

        String message = "INSERT INTO \"hub_entry\" (" +
                "\"hublist_id\", \"name\", \"address\", \"description\", users, \"country\", \"shared\", \"status\", \"min_share\", \"min_slots\", \"max_users\", \"max_hubs\", \"reliability\", \"rating\", \"port\") VALUES (" +
                "'"+hublist_id+"','"+name+"', '"+address+"', '"+description+"', '"+users+"', '"+country+"', '"+shared+"', '"+ status + "', '"+min_share+"', '"+min_slots+ "', '"+max_users + "', '"+max_hubs + "', '"+reliability+ "', '"+rating+ "', '"+port+"')";
        //lastMessage = message;
        db.writeRawSql(message,true);
    }
    
    private void updateHublist(Date now,Hublist hublist) throws SQLException, UnsupportedEncodingException{
        
        String query = "UPDATE hublist SET online="+hublist.isOnline()+",lastupdate='"+ now +"',check_count="+hublist.getCheck_count()+",online_count="+hublist.getOnline_count()+((hublist.getName()!=null)?",name='"+hublist.getName()+"'":"") +",dlsize = "+hublist.getDlsize()+",dtdwellformed="+hublist.isDtdValid()+",hubcount="+hublist.size()+
                ( (hublist.isOnline() )? ", lastonline='" : ", lastoffline='" ) + now +
                "' WHERE hublist.id = "+hublist.getId();
        
        db.writeUpdate(query);
    }
    
    private void commit(Date now, Hublist hublist)  {
        try {
            updateHublist(now,hublist);
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        
        Iterator<HubEntry> it = hublist.iterator();
        
        while(it.hasNext()){
            HubEntry hub = it.next();
            try {
                insertHubEntry(hublist.getId(),hub);
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
                //System.out.println(lastMessage);
            } catch (SQLException ex) {
                ex.printStackTrace();
                 //System.out.println(lastMessage);
            }
        }
    }
    

    /**
     * Escreve as Hublist's passadas como argumento na BD
     * @param hublists 
     */
    public void commit( LinkedList<Hublist> hublists) {
        Date now= new Date();
        try {
            db.emptyTable("hub_entry");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        for(Hublist hublist: hublists)
            commit(now,hublist);
    }

}

